package com.fu.paging;

import com.fu.bean.Student;
import com.fu.mapper.StudentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author 付大大
 * @description
 * @create 2021-02-01 16:18
 */
public class PageTest {

    @Test
    public void selectCondition() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        PageHelper.startPage(3, 3);
        List<Student> list = mapper.selectAll();

        for (Student student1 : list) {
            System.out.println(student1);
        }

        PageInfo<Student> info = new PageInfo<>(list);
        System.out.println("总条数" + info.getTotal());
        System.out.println("总页数" + info.getPages());
        System.out.println("当前页" + info.getPageNum());
        System.out.println("每页显示条数" + info.getPages());
        System.out.println("上一页" + info.getPrePage());
        System.out.println("下一页" + info.getNextPage());
        System.out.println("是否是第一页" + info.isIsFirstPage());
        System.out.println("是否是最后一页" + info.isIsLastPage());

        sqlSession.close();
        resourceAsStream.close();
    }
}
